package com.probox.apiModules.controller.product;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.google.gson.Gson;
import com.probox.common.core.enums.CategoryDepth;
import com.probox.common.core.enums.DataStatus;
import com.probox.common.core.exception.ProductException;
import com.probox.common.core.utils.DateUtils;
import com.probox.common.core.utils.SecurityUtils;
import com.probox.common.core.utils.StringUtils;
import com.probox.common.core.web.controller.BaseController;
import com.probox.common.core.web.domain.AjaxResult;
import com.probox.common.entity.domain.customer.LevelPrivilege;
import com.probox.common.entity.domain.customer.RechargeInfo;
import com.probox.common.entity.domain.product.ProductGroup;
import com.probox.common.entity.service.customer.*;
import com.probox.common.entity.vo.customer.CustomerLevelVo;
import com.probox.common.entity.vo.recharge.LunwenRechargeVo;
import com.probox.common.security.annotation.VerifyAuthorize;
import com.probox.common.entity.domain.product.ProductBuyersshow;
import com.probox.common.entity.domain.product.ProductCategory;
import com.probox.common.entity.domain.product.vo.*;
import com.probox.common.entity.service.order.IOrderInfoItemService;
import com.probox.common.entity.service.product.*;
import com.probox.customer.api.domain.CustomerCollect;
import com.probox.customer.api.domain.LevelAndPrivilege;
import com.probox.product.api.domain.LunWenVo;
import com.probox.product.api.domain.ProductGroupViewVo;
import com.probox.product.api.domain.ProductInfoVo;
import com.probox.product.api.domain.ZhiChengVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;


@Api(value = "商品模块", tags = {"商品信息前端接口"})
@RestController
@RequestMapping("/product")
public class OpenProductInfoController extends BaseController {

    @Autowired
    private IProductInfoService iProductInfoService;

    @Autowired
    private IProductCategoryService iProductCategoryService;


    @Autowired
    private IProductGroupService productGroupService;

    @Autowired
    private IRechargeInfoService rechargeInfoService;

    @Autowired
    private ICustomerPrivilegeService customerPrivilegeService;

    @Autowired
    private ICustomerCollectService customerCollectService;




    @ApiOperation(value = "储值信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductInfoVo.class),
    })
    @GetMapping("/recharge/list")
    public AjaxResult rechargeList() {
        List<LunwenRechargeVo> rst = new ArrayList<>();
        List<RechargeInfo> recharges = rechargeInfoService.selectByStroeId(null);
        for (RechargeInfo r:recharges) {
            LunwenRechargeVo vo = new LunwenRechargeVo();
            BeanUtils.copyProperties(r,vo);
            if (r.getMemberLevel() != null){
                List<LevelAndPrivilege> lap = customerPrivilegeService.getLevelAndPrivilege(r.getMemberLevel());
                List<String> rights = new ArrayList<>();
                LevelAndPrivilege jf = null;
                for (LevelAndPrivilege l:lap) {
                    if (l.getPrivilegeName().equalsIgnoreCase("points")){
                        jf = l;
                    }else{
                        if (StringUtils.isNotEmpty(l.getPlatformUnit())) {
                            rights.add(l.getPrivilegeShowName() + ":" + l.getPrivilegeValue() + l.getPlatformUnit()+"。");
                        }else{
                            rights.add(l.getPrivilegeShowName() + ":" + l.getPrivilegeValue()+"。");
                        }
                    }
                }
                if (jf != null){
                    rights.add(0,"兑换"+vo.getScore()+"积分，额外赠送"+jf.getPrivilegeValue()+"积分，共获得"+(vo.getScore()+Long.valueOf(jf.getPrivilegeValue()))+"积分。");
                    vo.setScore(vo.getScore()+Long.valueOf(jf.getPrivilegeValue()));
                }else{
                    rights.add(0,"兑换"+vo.getScore()+"积分。");
                }
                vo.setRights(rights);
            }
            rst.add(vo);
        }
        return AjaxResult.success(rst);
    }


    @ApiOperation(value = "查询论文信息列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductInfoVo.class),
    })
    @GetMapping("/infoApi/list")
    public AjaxResult selectInfo(LunWenVo lunWenVo) {
        List<LunWenVo> list = new ArrayList<>();
        startPage();
        list = iProductInfoService.select(lunWenVo);

        for (LunWenVo vo:list) {
            vo.setShowFile(null);
            vo.setCreateDate(DateUtils.formateToday(vo.getCreateTime()));
            vo.setCreateTime(null);
            vo.setJifen(iProductInfoService.getDownloadJiFen(vo));
            if (SecurityUtils.getCustomerUid() != null){
                CustomerCollect collect = new CustomerCollect();
                collect.setObjUid(vo.getProductUid());
                collect.setCustomerUid(SecurityUtils.getCustomerUid());
                vo.setIsCollect(customerCollectService.getIsCollectGoods(collect)?"1":"0");
            }
        }
        return AjaxResult.successs(list, iProductInfoService.selectCount(lunWenVo));
    }

    @ApiOperation(value = "查询职称论文列表")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductInfoVo.class),
    })
    @GetMapping("/infoApi/zhiCheng")
    public AjaxResult zhiCheng(LunWenVo lunWenVo) {
        List<ZhiChengVo> rst = new ArrayList<>();

        ProductCategory productCategory = new ProductCategory();
        productCategory.setPlatformUid(1L);
        productCategory.setDataStatus(DataStatus.valid.getKey());
        productCategory.setGroupUid(3L);
        List<ProductCategory> list = iProductCategoryService.queryList(productCategory);
        lunWenVo = new LunWenVo();
        for (ProductCategory g:list) {
            ZhiChengVo zc = new ZhiChengVo();
            zc.setMajorName(g.getCategoryName());
            zc.setMajorUid(g.getCategoryUid());
            startPage();
            lunWenVo.setMajorUidOne(g.getCategoryUid());
            zc.setLunWenList(iProductInfoService.select(lunWenVo));
            rst.add(zc);
        }
        return AjaxResult.success(rst);
    }

    /**
     * 其他类型的论文
     * @param lunWenVo
     * @return
     */
    @GetMapping("/infoApi/otherLuwn")
    public AjaxResult otherLuwn(LunWenVo lunWenVo) {
        List<ZhiChengVo> rst = new ArrayList<>();

        List<ProductGroup> groups = productGroupService.queryOtherCategoryList();

        lunWenVo = new LunWenVo();
        for (ProductGroup g:groups) {
            ZhiChengVo zc = new ZhiChengVo();
            zc.setMajorName(g.getGroupName());
            zc.setMajorUid(g.getGroupUid());
            startPage();
            lunWenVo.setCategoryUid(g.getGroupUid());
            zc.setLunWenList(iProductInfoService.select(lunWenVo));
            rst.add(zc);
        }
        return AjaxResult.success(rst);
    }



    @ApiOperation(value = "获取单个商品信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductInfoVo.class),
    })
    @GetMapping("/infoApi/{productUid}")
    public AjaxResult getProduct(@PathVariable("productUid") Long productUid) {
        return AjaxResult.success(iProductInfoService.getLunWenView(productUid));
    }


    //getCategoryVo
    @ApiOperation(value = "查询商品顶级分类下的所有子分级,以及商品的轮播图")
    @GetMapping("/categoryApi/childCategory/{categoryUid}")
    @ApiResponses({
            @ApiResponse(code = 200, message = "OK", response = ProductCategory.class),
    })
    public AjaxResult selectChildCategory(@PathVariable("categoryUid") Long categoryUid) {
        return AjaxResult.success(iProductCategoryService.getCategoryVo(categoryUid));
    }


}
